package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Buyer;
import com.shaoshaohuo.app.entity.PurchaseDetail.ProductMore;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.shaoshaohuo.app.utils.im.IMFactory;

import java.util.List;

public class MerchantsDetailListAdapter<T> extends MyBaseAdapter<Buyer> {

	public MerchantsDetailListAdapter(Context ctx, List<Buyer> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {
		
		ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context,R.layout.view_merchants_detail_list_item, null);
            holder = new ViewHolder();
            holder.titleText = (TextView) convertView.findViewById(R.id.textview_name);
            holder.dangkouText = (TextView) convertView.findViewById(R.id.textview_dangkou);
            holder.productText = (TextView) convertView.findViewById(R.id.textview_zhuyingchanpin);
            holder.yearMoneyText = (TextView) convertView.findViewById(R.id.textview_yearmoney);
            holder.callphoneImage = (ImageView) convertView.findViewById(R.id.imageview_call_phone);
            holder.callChatImage = (ImageView) convertView.findViewById(R.id.imageview_call_chat);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
		
        final Buyer item = list.get(pos);
        
        holder.titleText.setText(item.getRealname());
        holder.dangkouText.setText(item.getNumber());
        
        StringBuilder sb = new StringBuilder();
        for(ProductMore pro :item.getCategorys()){
            sb.append(pro.getCatname()+" ");
        }
        
        holder.productText.setText(sb.toString());
        holder.yearMoneyText.setText(item.getYearmoney()+"W");
        holder.callphoneImage.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                DeviceHelper.callPhone(context, item.getMobile(), item.getKey());
            }
        });
        holder.callChatImage.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
            	IMFactory.openChat(context, item.getUserid());
            }
        });
		return convertView;
	}
	
	static class ViewHolder{
		TextView titleText;
		TextView dangkouText;
		TextView productText;
		TextView yearMoneyText;
		ImageView callChatImage;
		ImageView callphoneImage;
	}
	
}
